'use strict';

module.exports = app => {
  const {
    STRING,
    INTEGER,
    DATE,
    BLOB,
  } = app.Sequelize;

  const User = app.model.define('user', {
    id: {
      type: INTEGER,
      allowNull: false,
      primaryKey: true,
      autoIncrement: true,
      comment: '序号',
    },
    username: {
      type: STRING(50),
      allowNull: true,
      comment: '姓名',
    },
    password: {
      type: BLOB,
      allowNull: false,
      comment: '密码',
    },
    userimg: {
      type: STRING(100),
      allowNull: false,
      comment: '头像',
    },
    tel: {
      type: STRING(20),
      allowNull: false,
      comment: '手机号码',
    },
    email: {
      type: STRING(50),
      comment: '邮箱',
    },
    nickname: {
      type: STRING(50),
      allowNull: false,
      unique: true,
      comment: '昵称',
    },
    sex: {
      type: INTEGER(2),
      allowNull: false,
      comment: '性别',
    },
    birthday: {
      type: DATE,
      comment: '生日',
    },
    provinceaddress: {
      type: STRING(50),
      comment: '省',
    },
    cityaddress: {
      type: STRING(50),
      comment: '市',
    },
    areaaddress: {
      type: STRING(50),
      comment: '县',
    },
    hobby: {
      type: STRING(50),
      comment: '爱好',
    },
    createdDate: {
      type: DATE,
      allowNull: false,
      comment: '入库时间',
      field: 'createdDate',
    },
    updatedDate: {
      type: DATE,
      allowNull: false,
      comment: '更新登录时间',
      field: 'updatedDate',
    },
  }, {
    freezeTableName: true,
    timestamps: false, // 去除createAt updateAt
  });
  User.associate = function() {
    app.model.User.hasMany(app.model.Receivingaddress, {
      foreignKey: 'id',
      targetKey: 'uid',
    });
    app.model.User.hasMany(app.model.TbOrder, {
      foreignKey: 'id',
      targetKey: 'uid',
    });
  };
  return User;
};
